<?php


namespace app\admin\controller\business;


use app\common\controller\Adminbase;
use think\Controller;
use think\Db;
use think\facade\Session;

class Finance extends Adminbase
{
    protected function initialize()
    {
        $this->where = array();
        if (Session::get("admin.roleid") == 3) {
            $admin_where[] = ['area_id', '=', Session::get("admin.area_id")];
            $admin_where[] = ['roleid', '=', 4];
            $ying_ids=Db::name('admin')->where($admin_where)->column('id');
            $business_ids = Db::name('business')
                ->where('belong_admin_id', 'in',$ying_ids)
                ->column('id');
            $this->where[] = ['bf.business_id', 'in', $business_ids];
        }
        if (Session::get("admin.roleid") == 4) {
            $business_ids = Db::name('business')->where('belong_admin_id', Session::get("admin.id"))->column('id');
            $this->where[] = ['bf.business_id', 'in', $business_ids];
        }
        parent::initialize();
    }

    public function index()
    {
        $status = input('param.status/d');
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $where2[] = ['bf.type', '=', 4];
            if (isset($status)) {
                $where2[] = ['bf.status', '=', $status];
            }
            //status '0未完成 待审核 1已完成 审核完成'
            //type 1.推广 2线上销售 3进货 4提现
            $_list = Db::name('business_finance')
                ->alias('bf')
                ->join('business b', 'bf.business_id=b.id')
                ->field('bf.*,b.business_name,b.phone')
                ->where($where2)
                ->where($this->where)
                ->order('create_time desc')
                ->page($page, $limit)
                ->select();
            $total = Db::name('business_finance')
                ->alias('bf')
                ->join('business b', 'bf.business_id=b.id')
                ->field('bf.*,b.business_name,b.phone')
                ->where($this->where)
                ->where($where2)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if($_list[$k]['status_time']){
                    $_list[$k]['status_time'] = date('Y-m-d H:i:s', $v['status_time']);
                }else{
                    $_list[$k]['status_time']='待审核';
                }
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        if (isset($status)) {
            $this->assign('status', $status);
        }
        return $this->fetch('business/finance/index');
    }

    public function detail()
    {
        if ($this->request->isPost()) {
            $finance_id = $this->request->param('id/d', 0);
            $params = $this->request->post("row/a");
            $update = array(
                'id' => $finance_id,
                'status' => $params['status'],
            );
            if ($params['status']) {
                $update['status_time'] = time();
            }
            $num = Db::name('business_finance')->update($update);
            $this->success("提交成功", url("index"));
            return $params;
        } else {
            $finance_id = $this->request->param('id/d', 0);
            $type = $this->request->param('type/d', 0);
            $data = Db::name('business_finance')
                ->alias('bf')
                ->join('business b', 'bf.business_id=b.id')
                ->field('bf.*,b.business_name,b.phone')
                ->where('bf.id', '=', $finance_id)
                ->order('create_time desc')
                ->find();
            switch ($data['tixian_type']){
                case 1:
                    $data['tixian_type_name']='微信提现';
                    break;
                case 2:
                    $data['tixian_type_name']='支付宝';
                    break;
                case 3:
                    $data['tixian_type_name']='银行提现';
                    break;
                default:
                    $data['tixian_type_name']='其他';
            }
            $this->assign("data", $data);
            $this->assign("type", $type);
            return $this->fetch('business/finance/edit');
        }
    }
}